﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Broker.Model;
using BrokerBase.Lib.Tool;
using NHibernate;
using WCFDynamicProxyLibrary.DynamicProxy;
using BrokerBase.Lib.Client;
using Broker.Comm;
using BrokerBase.Lib.Core;
using Broker.Update;

namespace Broker
{
    /// <summary>
    /// UserInfoPage.xaml 的交互逻辑
    /// </summary>
    public partial class UserInfoPage : Page
    {
        private LicenseInfo licInfo;
        private SysUser user;
        private IList<ScoreRecordInfo> scoreInfos;
        private IList<string> urls;
        private UserInfoForm owner;
        public UserInfoPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(UserInfoPage_Loaded);
        }

        public UserInfoPage(UserInfoForm owner):this()
        {
            this.owner = owner;
        }

        void UserInfoPage_Loaded(object sender, RoutedEventArgs e)
        {
            //AppGlobal.DataWCF.ConnectStateChanged += new ConnectStateChangedHandler(DataWCF_ConnectStateChanged);
            //AppGlobal.DataWCF.ClientRegInfoChanged += new ClientRegInfoChangedHandler(DataWCF_ClientRegInfoChanged);

            //AppGlobal.DataWCF.ClientScoreReceived += new ClientScoreInfoReceivedHandler(DataWCF_ClientScoreReceived);

            user = AppGlobal.CurrentUser;

            urls = new List<string> { AppGlobal.ClientCfg.ServerUrl1, AppGlobal.ClientCfg.ServerUrl2 };

            if (user == null)
                user = new SysUser();

            DataContext = user;

            licInfo = LicenseInfo.GetInstance();

            tabLicenseInfo.DataContext = licInfo;

            string s = txtcode.Text;

            //LoadData();

            //SetInviteState();
        }

        //void DataWCF_ConnectStateChanged(RemoteDataServiceState state)
        //{
        //    this.Dispatcher.Invoke(new Action(SetInviteState));
        //}

        //void DataWCF_ClientScoreReceived()
        //{
        //    this.Dispatcher.Invoke( new Action(()=>
        //        {
        //            grdPrompt.Visibility = Visibility.Visible;
        //        }));
        //}

        //void DataWCF_ClientRegInfoChanged(ClientDataInfo info)
        //{
        //    this.Dispatcher.Invoke(new Action(() =>
        //        {
        //            SetInviteQty();
        //        }));
        //}

        //private void SetInviteState()
        //{
        //    switch (AppGlobal.DataWCF.State)
        //    {
        //        case RemoteDataServiceState.NoConnect:
        //            tbkState.Text = "没有连接到服务器，或服务器连接出现异常。";
        //            break;
        //        case RemoteDataServiceState.Connecting:
        //            tbkState.Text = "正在连接服务器,请等待，大概需要1到5分钟。";
        //            break;
        //        case RemoteDataServiceState.Received:
        //            SetInviteQty();
        //            break;
        //    }
        //}

        //private void SetInviteQty()
        //{
        //    int invQty = AppGlobal.ResultClientRegInfo.InviteQty;
        //    if (invQty > 0)
        //    {
        //        tbkState.Text = "您还有"+invQty+"个可用邀请码。";
        //        btnInvite.IsEnabled = true;
        //    }
        //    else
        //    {
        //        tbkState.Text = "您已经没有可用邀请码！";
        //        btnInvite.IsEnabled = false;
        //    }

        //    txtInviteBtn.Text = string.Format("邀请您的朋友一起使用!还剩({0})个注册码", invQty);
        //}

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {


            if (user == null)
            {
                MessageBox.Show("软件还未注册，用户无法保存!");
            }
            else
            {
                try
                {
                    DBServiceUtil.GetService<SysUser>(AppGlobal.HbmSessionFactory.GetCurrentSession()).SaveOrUpdate(user);

                    MessageBox.Show("保存成功!");
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
            }
        }

        private void btnPassword_Click(object sender, RoutedEventArgs e)
        {
            if (user != null && user.Id >0)
            {
                UserPasswordForm frm = new UserPasswordForm(user);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = AppGlobal.MainWin;
                frm.ShowDialog();

            }
            else
            {
                MessageBox.Show("产品还没有注册，不能修改登录口令","提示");
            }
        }

        private void btnReg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RegisterForm frmReg = new RegisterForm(licInfo);

                frmReg.CallBack = (bool isSuccess) =>
                    {
                        txtName.Text = licInfo.UserName;

                        tabLicenseInfo.DataContext = null;
                        tabLicenseInfo.DataContext = licInfo;
                    };

                frmReg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frmReg.Owner = AppGlobal.MainWin;
                
                frmReg.ShowDialog();

            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
        }

        //private void LoadData()
        //{

        //    scoreInfos = DBServiceUtil.GetService<ScoreRecordInfo>(AppGlobal.HbmSessionFactory.GetCurrentSession()).GetListByHQL("from ScoreRecordInfo as s Order by s.ScoreTime Desc").ToList<ScoreRecordInfo>();

        //    gvScoreInfo.ItemsSource = scoreInfos;

        //    txtScore.Text = scoreInfos.Sum(p => p.Score).ToString();
        //}

        private void btnInvite_Click(object sender, RoutedEventArgs e)
        {
            InvitedForm frm = new InvitedForm();

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = AppGlobal.MainWin;
            frm.ShowDialog();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            owner.Close();
        }

        //private void Hyperlink_Click(object sender, RoutedEventArgs e)
        //{
        //    using (ISession sess = AppGlobal.HbmSessionFactory.OpenSession())
        //    {
        //        LoadData();
        //        AppGlobal.DataWCF.IsNewReg = false;
        //        grdPrompt.Visibility = Visibility.Collapsed;
        //    }
        //}
    }
}
